﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WikiTrailCreator.TrailData;
using WikiTrailCreator.Properties;

namespace WikiTrailCreator.WikiGeneration
{
    public partial class FormWikiGeneration : Form
    {
        private WikiPage mainPage;
        private WikiPage galleryPage;
        private PhotoWikiPage[] photoPages;

        public FormWikiGeneration()
        {
            InitializeComponent();
        }

        public void SetTrail(Trail trail)
        {
            TrailToWikiPageConvertor.GetWikiPages(trail, out mainPage, out galleryPage, out photoPages);

            listBoxPages.Items.Clear();

            // Set the pages into the trail list
            if (trail.TrailName != String.Empty)
            {
                if (mainPage != null)
                    listBoxPages.Items.Add(mainPage);

                if (galleryPage != null)
                    listBoxPages.Items.Add(galleryPage);

                if (photoPages != null && photoPages.Length > 0)
                    listBoxPages.Items.AddRange(photoPages);
            }

            listBoxPages_SelectedValueChanged(this, new EventArgs());
        }

        private void listBoxPages_SelectedValueChanged(object sender, EventArgs e)
        {
            WikiPage wikiPage = listBoxPages.SelectedItem as WikiPage;

            if (wikiPage != null)
            {
                if (wikiPage is PhotoWikiPage)
                {


                    ucWikiPhotoPanel wikiPhotoPanel = new ucWikiPhotoPanel();
                    wikiPhotoPanel.Dock = DockStyle.Fill;
                    wikiPhotoPanel.PhotoWikiPage = wikiPage as PhotoWikiPage;
                    groupBoxDetails.Controls.Clear();
                    groupBoxDetails.Controls.Add(wikiPhotoPanel);
                }
                else
                {
                    ucWikiTextPanel wikiTextPanel = new ucWikiTextPanel();
                    wikiTextPanel.Dock = DockStyle.Fill;
                    wikiTextPanel.WikiPage = wikiPage;
                    groupBoxDetails.Controls.Clear();
                    groupBoxDetails.Controls.Add(wikiTextPanel);
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            // Prompt for the activation key...
            FormActivationCode formActivationCode = new FormActivationCode();
            formActivationCode.TrailName = mainPage.Name;
            if (formActivationCode.ShowDialog(this) != DialogResult.OK)
                return;
            
            // Show the progress bar...
            ProgressBarForm.ShowDialogInSeparateThread(this);

            // Count the steps...
            int numPhotoPages = (photoPages != null) ? photoPages.Count() : 0 ;
            ProgressBarForm.SetMaxProgress(numPhotoPages + 3);

            try
            {
                // Login...
                ProgressBarForm.SetDisplayText("Logging in to " + Settings.Default.WikiURL + "...");
                WikiSession wikiSession = new WikiSession(Settings.Default.WikiURL);
                bool bLoggedIn = wikiSession.Login(Settings.Default.WikiUserName, Settings.Default.WikiPassword);
                ProgressBarForm.IncCurrentProgress();

                if (bLoggedIn)
                {
                    // Create the main page...
                    ProgressBarForm.SetDisplayText("Creating " + mainPage.Name + "...");
                    bool bSuccess = wikiSession.CreatePage(mainPage.Name, mainPage.PageWikiCode);
                    if (!bSuccess)
                    {
                        DialogResult dialogResult = MessageBox.Show(this, "Failed to create " + mainPage.Name + ". Would you like to continue building the rest of the pages?", "Error", MessageBoxButtons.YesNo);
                        if (dialogResult == DialogResult.No)
                            return;
                    }
                    ProgressBarForm.IncCurrentProgress();

                    // Create the photo gallery page...
                    if (galleryPage != null)
                    {
                        ProgressBarForm.SetDisplayText("Creating " + galleryPage.Name + "...");
                        bSuccess = wikiSession.CreatePage(galleryPage.Name, galleryPage.PageWikiCode);
                        if (!bSuccess)
                        {
                            DialogResult dialogResult = MessageBox.Show(this, "Failed to create " + galleryPage.Name + ". Would you like to continue building the rest of the pages?", "Error", MessageBoxButtons.YesNo);
                            if (dialogResult == DialogResult.No)
                                return;
                        }
                        ProgressBarForm.IncCurrentProgress();
                    }

                    // Upload the photos...
                    if (photoPages != null)
                    {
                        foreach (PhotoWikiPage photoPage in photoPages)
                        {
                            ProgressBarForm.SetDisplayText("Uploading image " + photoPage.Name + "...");
                            bSuccess = wikiSession.UploadImage(photoPage.Name, photoPage.LocalImagePath, photoPage.PageWikiCode);
                            if (!bSuccess)
                            {
                                DialogResult dialogResult = MessageBox.Show(this, "Failed to upload the image " + photoPage.LocalImagePath + ". Would you like to continue building the rest of the pages?", "Error", MessageBoxButtons.YesNo);
                                if (dialogResult == DialogResult.No)
                                    return;
                            }
                            ProgressBarForm.IncCurrentProgress();
                        }
                    }

                    ProgressBarForm.CloseForm();
                    MessageBox.Show(this, "All requested pages were generated, please verify in a web browser that your trail was successfully created.", "Success", MessageBoxButtons.OK);
                    this.Close();
                }
                else
                {
                    ProgressBarForm.CloseForm();
                    MessageBox.Show(this, "Could not log into " + Settings.Default.WikiURL + " with the user name and password provided. Please check these under the File->Settings menu and make sure that they are set properly.","Error", MessageBoxButtons.OK);
                }
            }
            finally
            {
                ProgressBarForm.CloseForm();
            }
        }
    }
}
